<?php

namespace App\Kuafu\Shop\Service;

use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\Common\Service\InformationPushService;
use App\Kuafu\CouponExt\Channel\CouponExtBase;
use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\Shop\Exceptions\ShopException;
use App\Kuafu\User\Service\UserRoleService;
use App\Kuafu\WaiMai\Enum\WaiMaiCodeEnum;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Kuafu\WaiMai\Exceptions\WaiMaiException;
use App\Kuafu\WaiMai\Model\WaiMaiShopMappingModel;
use App\Remote\Tiger\Appraise\AppraiseRemote;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;


class DouyinShopService extends BaseService
{
    public static function handleDouYinShopListV2($thirdShopId)
    {
        $dyModel = new \App\Kuafu\Shop\Service\DouyinLibrary\DouyinShopService();

        $shopInfo = $dyModel->getShopByPoiId($thirdShopId);

        if ($shopInfo['error_code'] != 0) {
            return [];
        }

        $result                  = $shopInfo['pois'][0]['poi'];
        $result['thirdShopName'] = $result['poi_name'];
        $result['thirdShopId']   = $result['poi_id'];

        unset($result['poi_name']);
        unset($result['poi_id']);

        return [$result];
    }

    public static function bandDouYinShopMapping($sourceType, $shopId, $thirdShopId, $thirdShopName)
    {
        $shop = ShopService::getShopInfo($shopId);
        if (!$shop) {
            throw new ShopException(ShopCodeEnum::ERR_SHOP_ID_NO_EXIST);
        }
        // 检查thirdShopId是否已被绑定
        $checkShopId = WaiMaiShopMappingModel::getShopIdByThirdShopId($thirdShopId, $sourceType);
        if ($checkShopId) {
            throw new WaiMaiException(WaiMaiCodeEnum::ERR_THIRD_SHOP_ID_EXISTS,
                "该三方门店id已被店铺ID:{$checkShopId}绑定");
        }

        if (empty($thirdShopName)) {
            $thirdShopName = $shop->name;
        }


        DB::beginTransaction();
        try {
            WaiMaiShopMappingModel::addData(
                $sourceType,
                $shopId,
                $thirdShopId,
                $thirdShopName
            );

            // 抖音外卖
            WaiMaiShopMappingModel::addData(
                WaiMaiDBEnum::SOURCE_TYPE_DOUYIN_WM,
                $shopId,
                $thirdShopId,
                $thirdShopName
            );
            DB::commit();
        } catch (\Exception $exception) {
            DB::rollBack();
            Log::error('绑定抖音三方门店失败', [
                'msg'           => $exception->getMessage(),
                'shopId'        => $shopId,
                'thirdShopId'   => $thirdShopId,
                'thirdShopName' => $thirdShopName,
            ]);
            throw new WaiMaiException(WaiMaiCodeEnum::ERR_DB_ADD,
                "绑定抖音三方门店失败");
        }

        $userIds = UserRoleService::getShopManageList(
            config('merchant.manager_role_id'),
            [$shopId]
        )['lists'] ?? [];

        InformationPushService::push(
            InformationPushService::DY_GROUP_OPEN_PUSH,
            array_column($userIds, 'userId'),
            $shop
        );
        //日常运营阶段 饿了么外卖上线&美团外卖上线「需推送」 点评团购上线&抖音团购上线「需推送」
        AppraiseRemote::waimaiNotify($shopId, $sourceType, $thirdShopId);
    }

    public static function unBandDouYinShopMapping($sourceType, $shopId)
    {
        $shop = ShopService::getShopInfo($shopId);
        if (!$shop) {
            throw new ShopException(ShopCodeEnum::ERR_SHOP_ID_NO_EXIST);
        }
        // 检查店铺是都绑定了抖音
        $checkShopId = WaiMaiShopMappingModel::getDataByShopId($shopId, $sourceType);
        if (!$checkShopId) {
            throw new WaiMaiException(WaiMaiCodeEnum::ERR_NOT_BAND_DOUYIN);
        }
        WaiMaiShopMappingModel::delData($shopId, $sourceType);
        WaiMaiShopMappingModel::delData($shopId, WaiMaiDBEnum::SOURCE_TYPE_DOUYIN_WM);
    }


}
